
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

//base class
#include "IPrimitive.h"

//This class represent a triangle
class CPrimTriangle : public IPrimitive
{
public:

	//the determinant used to solve the uv map
	float _D;

	//flag set to indicate if a translation is needed during uv map solving
	bool _useTranslation;

	//the three points translated
	CVector3f _uvTranslated[3];

	//the translation vector used to offset
	CVector3f _offset;

private:
	//the tree vertex composing the triangle
	CVector3f m_vertex[3];

	//The three uv map coordinate (x = U, y = V, w = nothing)
	CVector3f m_UV[3];

	//Triangle normal
	CVector3f m_normal;

	
public:
	//constructor
	CPrimTriangle();

	//constructor
	CPrimTriangle(CVector3f* v);

	//constructor
	CPrimTriangle(CVector3f v1, CVector3f v2, CVector3f v3);

	//destructor
	~CPrimTriangle();

	//add a vertex
	void addVertex(const CVector3f& v, int id);

	//add a uv texture coordinate
	void addUV(const CVector3f& v, int id);

	//set the normal
	void setNormal(const CVector3f& n);

	//calculate the normal
	void computeNormal();

	//precompute some value to make calculation faster
	void preCompute();

	//calculate if there is an intersection between ray and the triangle
	bool intersect(const CRay& ray, float& dist, bool useGlobalCoordinates);

	//calculate if there is an intersextion between the ray and the triangle. The ray must be in triangle local space.
	bool intersectLocalSpace(const CRay& ray, float& dist);

	void computeNormal(const CRay& ray, float dist, CVector3f& normal)const;
	bool isInside(const CVector3f& p)const;

	//get a pointer to the vertex array
	CVector3f* getVertices();

	//get a pointer to the uv map array
	CVector3f* getUV();

private:
	bool sameSide(const CVector3f& p1, const CVector3f& p2, const CVector3f& a, const CVector3f& b)const;

};